import { isHotkey } from 'is-hotkey';

import { TextType, CustomEditor, ElementType, IndentType } from './slate';

import { toggleMark, toggleIndent } from './command';

type ELEMENT_HOTKEYS = Record<string, ElementType>;
type MARK_HOTKEYS = Record<string, TextType>;

export const MARK_HOTKEYS: MARK_HOTKEYS = {
  'mod+b': 'bold',
  'mod+i': 'italic',
  'mod+u': 'underline',
  'mod+`': 'code',
};

type INDENT_HOTKEYS = Record<string, IndentType>;

export const INDENT_HOTKEYS: INDENT_HOTKEYS = {
  tab: 'right',
  'shift+tab': 'left',
};

export const hotkeyCall = (editor: CustomEditor) => (event: React.KeyboardEvent) => {
  Object.keys(MARK_HOTKEYS).forEach((hotkey) => {
    if (isHotkey(hotkey, event.nativeEvent)) {
      event.preventDefault();
      toggleMark(editor, MARK_HOTKEYS[hotkey]);
    }
  });
  Object.keys(INDENT_HOTKEYS).forEach((hotkey) => {
    if (isHotkey(hotkey, event.nativeEvent)) {
      event.preventDefault();
      toggleIndent(editor, INDENT_HOTKEYS[hotkey]);
    }
  });
};
